<script setup>
	import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'

	import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
	import {
		ref
	} from 'vue'
	import dayjs from 'dayjs'
	import optionDeptPerson from '@/store/modules/optionDeptPerson'
	import {
		addAdministrativeApply,
		attendanceApplyApi,
		selectOrderNoApi,
	} from '@/api/attendance'
	import {
		getDeptTree
	} from '@/api/dep'
	import {
		getUserProfile
	} from '@/api/system/user'
	import store from '@/store'
	import approvalProcessState from '@/store/modules/approvalProcess'
	import {
		isObjectValuesNotEmpty
	} from '@/utils/objIsnull.js'
	const title = '用车'
	const carTypeRef = ref(null)
	const dateRef = ref(null)
	const date2Ref = ref(null)
	const userId = ref(null)
	const deptId = ref(null)
	const ItemRequisitionList = ref({
		carType: '',
		date: '',
		useTime: '',
		destination: '',
		reason: '',
		date2: '',
	})

	const carTypeColumns = [
		['车型1', '车型2', '车型3', '其他'],
	]
	const carTypeConfirm = (e) => {
		ItemRequisitionList.value.carType = e.value[0]
	}
	const carTypeFn = () => {
		carTypeRef.value.open()
	}
	const formatter = (type, value) => {
		if (type === 'year') {
			return `${value}年`
		}
		if (type === 'month') {
			return `${value}月`
		}
		if (type === 'day') {
			return `${value}日`
		}
		if (type === 'hour') {
			return `${value}时`
		}
		if (type === 'minute') {
			return `${value}分`
		}
		return value
	}

	const dateFn = () => {
		dateRef.value.open()
	}
	let datePicker = dayjs()
	const dateConfirm = (obj) => {
		ItemRequisitionList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
	}

	const date2Fn = () => {
		if (ItemRequisitionList.value.date) {
			date2Ref.value.open()
		} else {
			uni.showToast({
				title: '请先选择开始时间',
				icon: 'none'
			})
		}
	}
	let date2Picker = dayjs()
	const date2Confirm = (obj) => {
		ItemRequisitionList.value.date2 = dayjs(obj.value).format('YYYY-MM-DD')
	}

	const associatedTicketsFn = () => {
		uni.navigateTo({
			url: '/components/AssociatedTickets',
		})
	}

	const clickSave = async () => {
		const RequiredItemList = JSON.parse(JSON.stringify(ItemRequisitionList.value))
		delete RequiredItemList.carType
		if (isObjectValuesNotEmpty(RequiredItemList)) {
			let params = {
				carType: ItemRequisitionList.value.carType,
				startTime: dayjs(ItemRequisitionList.value.date).format('YYYY-MM-DD hh:mm:ss'),
				destination: ItemRequisitionList.value.destination,
				endTime: dayjs(ItemRequisitionList.value.date2).format('YYYY-MM-DD hh:mm:ss'),
				drivingTime: ItemRequisitionList.value.useTime,
				reason: ItemRequisitionList.value.reason,
				ruleId: 'yongche',
				type: '用车',
				userId: userId.value,
				deptId: deptId.value,
				approve: approvalProcessState.state.approvalProcess,
			}
			let {
				code
			} = await addAdministrativeApply(params)
			if (code === 200) {
				uni.showToast({
					title: '提交成功',
					duration: 1500,
					icon: 'success',
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			}
		} else {
			uni.showToast({
				title: '请输入完整数据',
				icon: 'error'
			})
		}
	}

	const getUserProfileFn = async () => {
		let {
			code,
			data
		} = await getUserProfile()
		if (code === 200) {
			userId.value = data.userId
			deptId.value = data.deptId
		}
	}
	const goHistoryView = () => {
		uni.navigateTo({
			url: '/components/TypeDefineModel?type=用车&apiName=行政',
		})
	}
	onLoad(async () => {
		await getUserProfileFn()
	})
	const listAssociateFile = ref([])
	const callBackAssociateData = () => {
		listAssociateFile.value = approvalProcessState.state.associateData
	}
	onShow(() => {
		callBackAssociateData()
	})
</script>

<template>
	<view>
		<!--    用车  -->
		<HeaderNavTabarSvg :title="title">
			<image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
		</HeaderNavTabarSvg>

		<view class="textarea-box">
			<text class="info-list-left isrequired">用车事由</text>
			<textarea v-model="ItemRequisitionList.reason" class="textinfo-list" placeholder="请输入"></textarea>
		</view>

		<view class="picker-content" @tap="dateFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">开始时间</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="ItemRequisitionList.date">{{ ItemRequisitionList.date }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="dateRef" v-model="datePicker" :formatter="formatter" mode="date" @confirm="dateConfirm">
		</uv-datetime-picker>

		<view class="picker-content" @tap="date2Fn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">结束时间</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="ItemRequisitionList.date2">{{ ItemRequisitionList.date2 }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="date2Ref" :min-date="ItemRequisitionList.date" v-model="date2Picker" :formatter="formatter"
			mode="date" @confirm="date2Confirm">
		</uv-datetime-picker>

		<view class="picker-content">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">用车时长</text>
				<view class="info-list-right">
					<view class="info-content">
						<input adjust-position type="text" class="uni-input" placeholder="请输入"
							v-model="ItemRequisitionList.useTime" />
					</view>
				</view>
			</view>
		</view>

		<view class="picker-content">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">用车目的地</text>
				<view class="info-list-right">
					<view class="info-content">
						<input adjust-position type="text" class="uni-input" placeholder="请输入"
							v-model="ItemRequisitionList.destination" />
					</view>
				</view>
			</view>
		</view>

		<view class="picker-content" @tap="carTypeFn">
			<view class="measurement-info-list">
				<text class="info-list-left">车辆类型</text>
				<view class="info-list-right">
					<view class="uni-input" v-if="ItemRequisitionList.carType">{{ ItemRequisitionList.carType }}
					</view>
					<view class="uni-inputs" v-else>请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>

		<uv-picker ref="carTypeRef" :columns="carTypeColumns" @confirm="carTypeConfirm"></uv-picker>

		<!--    审批流程-->
		<AuditApplicationSteps :ruleId="'yongche'" />
		<!--    底部占位符-->
		<view class="blank"></view>
		<!--    保存-->
		<view class="buttonContainer">
			<button class="buttonClass" @click="clickSave">提交</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	@import '@/static/style/index.scss';

	.associateClass {
		padding: 20rpx;
		background-color: #fff;

		.list-associate-file {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx;
			border-radius: 8rpx;
			background: #f2f3f5;
			margin-top: 20rpx;

			.list-associate-file-item-left {
				display: flex;
				align-items: center;

				image {
					width: 48rpx;
					height: 48rpx;
				}

				.content-item {
					margin-left: 20rpx;

					.item-left {
						display: flex;
						align-items: center;
					}

					h4 {
						align-self: stretch;
						color: #1d2129;
						font-size: 24rpx;
						font-style: normal;
						font-weight: 400;
						margin-right: 10rpx;
					}

					p {
						align-self: stretch;
						color: #86909c;
						font-size: 22rpx;
						font-style: normal;
						font-weight: 400;
					}
				}
			}

			.list-associate-file-item-right {
				border-radius: 32rpx;
				background: #C4C6C5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}
	}

	.card-list {
		padding: 24rpx 32rpx;
		width: 686rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		flex-direction: column;
		background-color: #ffffff;
		border-radius: 16rpx;
		border: 1rpx solid #e5e6eb;
		margin-bottom: 16rpx;

		.header-content,
		.main-content,
		.main-floor {
			@include flex($justify: space-between);
			color: #86909c;
			font-size: 24rpx;
			font-weight: 400;
		}

		.main-content {
			margin: 10rpx 0;

			h4 {
				color: #1d2129;
				font-size: 32rpx;
			}

			p {
				@include flex($justify: center, $align: center);
				padding: 0 8rpx;
				border-radius: 10rpx;
				min-width: 97rpx;
			}

			.待设计,
			.未下单,
			.待核算,
			.待上传安装图片,
			.待上传拆除照片 {
				color: #165dff;
				background-color: #e8f3ff;
			}

			.已核算,
			.已下单,
			.审核通过,
			.已通过 {
				color: #00b42a;
				background-color: #e8ffea;
			}

			.待审核 {
				color: #ff7d00;
				background-color: #fff7e8;
			}

			.驳回 {
				color: $btn-error-color;
				background-color: $btn-error-bg;
			}
		}

		.main-floor {
			.floot-svg {
				@include flex($align: center);

				image {
					width: 28rpx;
					height: 28rpx;
				}

				text {
					color: #1d2129;
				}
			}
		}

		.btn-install-order-list {
			margin-top: 20rpx;

			button {
				height: 56rpx;
				@include flex($justify: center, $align: center);
				border: none;
			}

			.btn-flex {
				@include flex($justify: space-between, $align: center);

				.接单 {
					background-color: #165dff;
					color: #fff;
				}

				button {
					width: 291rpx;
				}
			}

			.进行中 {
				background-color: #e8f3ff;
				color: #165dff;
			}

			.已拒接,
			.拒接 {
				background-color: #ffece8;
				color: #f53f3f;
			}

			.已完工 {
				background-color: #e8ffea;
				color: #00b42a;
			}

			.已超时 {
				background-color: #fff7e8;
				color: #ff7d00;
			}

			.row-btn {
				width: 100%;
			}
		}
	}

	.bukaSvg {
		width: 32rpx;
		height: 32rpx;
	}

	.buttonContainer {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		width: 100%;
		align-items: center;
		padding: 16rpx;
		background-color: #fff;
	}

	.buttonClass {
		display: flex;
		width: 686rpx;
		height: 72rpx;
		justify-content: center;
		align-items: center;
		background: #165dff;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
		//margin-top: 40rpx;
		overflow: hidden;
	}

	.uni-inputs {
		color: #86909c !important;
	}

	.uni-input {
		text-align: right;
		font-size: 28rpx;
	}

	.blank {
		height: calc(72rpx + 32rpx + 20rpx);
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.bukabanci {
		.info-content {
			color: #86909c !important;
		}

		.info-list-left {
			// @include font-color-auxiliary;
			color: #86909c !important;
		}
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.popup-content {
		background: #f7f8fa;

		.header-box {
			padding: 30rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			p {
				color: #1d2129;
				font-size: 34rpx;
				font-weight: 500;
			}

			.module-image-ba {
				border-radius: 32rpx;
				background: #f2f3f5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}
	}

	.measurement-info-list {
		@include flex($align: center, $justify: space-between);
		padding: $spacing-row-base;
		background: #fff;
		margin-top: 16rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.info-list-right {
			@include flex($align: center);

			.info-content {
				@include textStyleBase;
				font-size: 34rpx;
			}

			.iconfont {
				margin-left: 16rpx;
				// @include font-color-auxiliary;
				color: $uni-fc-fu-lin;
			}
		}

		.unit {
			// @include font-color-auxiliary;
			color: $uni-fc-fu-lin;
		}
	}
</style>
<!--  -->